//
//  Copyright (C) 2001-2009 Autodesk, Inc. All Rights Reserved.
//
//  By using this code, you are agreeing to the terms and conditions of
//  the License Agreement included in the documentation for this code.
//
//  AUTODESK MAKES NO WARRANTIES, EXPRESS OR IMPLIED, AS TO THE
//  CORRECTNESS OF THIS CODE OR ANY DERIVATIVE WORKS WHICH INCORPORATE
//  IT. AUTODESK PROVIDES THE CODE ON AN "AS-IS" BASIS AND EXPLICITLY
//  DISCLAIMS ANY LIABILITY, INCLUDING CONSEQUENTIAL AND INCIDENTAL
//  DAMAGES FOR ERRORS, OMISSIONS, AND OTHER PROBLEMS IN THE CODE.
//
//  Use, duplication, or disclosure by the U.S. Government is subject
//  to restrictions set forth in FAR 52.227-19 (Commercial Computer
//  Software Restricted Rights) and DFAR 252.227-7013(c)(1)(ii)
//  (Rights in Technical Data and Computer Software), as applicable.


#include "stdafx.h"
#include "MapInfoCreateSpatialContext.h"

MapInfoCreateSpatialContext::MapInfoCreateSpatialContext()
: MapInfoCommand<FdoICreateSpatialContext>(NULL)
{
    m_connection = NULL;
    m_extentType = FdoSpatialContextExtentType_Static;
    m_extent = NULL;
    m_xyTolerance = 0;
    m_zTolerance = 0;
    m_updateExisting = false;
}

MapInfoCreateSpatialContext::MapInfoCreateSpatialContext(MapInfoConnection* connection)
: MapInfoCommand<FdoICreateSpatialContext>(connection)
{
    m_extentType = FdoSpatialContextExtentType_Static;
    m_extent = NULL;
    m_xyTolerance = 0;
    m_zTolerance = 0;
    m_updateExisting = false;
}

MapInfoCreateSpatialContext::~MapInfoCreateSpatialContext()
{
}

FdoString* MapInfoCreateSpatialContext::GetName()
{
    return m_scName;
}

void MapInfoCreateSpatialContext::SetName(FdoString* value)
{
    m_scName = value;
}

FdoString* MapInfoCreateSpatialContext::GetDescription()
{
    return m_description;
}

void MapInfoCreateSpatialContext::SetDescription(FdoString* value)
{
    m_description = value;
}

FdoString* MapInfoCreateSpatialContext::GetCoordinateSystem()
{
    return m_coordSysName;
}

void MapInfoCreateSpatialContext::SetCoordinateSystem(FdoString* value)
{
    m_coordSysName = value;
}

FdoString* MapInfoCreateSpatialContext::GetCoordinateSystemWkt()
{
    return m_coordSysWkt;
}

void MapInfoCreateSpatialContext::SetCoordinateSystemWkt(FdoString* value)
{
    m_coordSysWkt = value;
}

FdoSpatialContextExtentType MapInfoCreateSpatialContext::GetExtentType()
{
    return m_extentType;
}

void MapInfoCreateSpatialContext::SetExtentType(FdoSpatialContextExtentType value)
{
    m_extentType = value;
}

FdoByteArray* MapInfoCreateSpatialContext::GetExtent()
{
    return FDO_SAFE_ADDREF( m_extent.p );
}

void MapInfoCreateSpatialContext::SetExtent(FdoByteArray* value)
{
    m_extent = FDO_SAFE_ADDREF( value );
}

const double MapInfoCreateSpatialContext::GetXYTolerance()
{
    return m_xyTolerance;
}

void MapInfoCreateSpatialContext::SetXYTolerance(double value)
{
    m_xyTolerance = value;
}

const double MapInfoCreateSpatialContext::GetZTolerance()
{
    return m_zTolerance;
}

void MapInfoCreateSpatialContext::SetZTolerance(const double value)
{
    m_zTolerance = value;
}

const bool MapInfoCreateSpatialContext::GetUpdateExisting()
{
    return m_updateExisting;
}

void MapInfoCreateSpatialContext::SetUpdateExisting(const bool value)
{
    m_updateExisting = value;
}

void MapInfoCreateSpatialContext::Execute()
{
    if ( m_coordSysWkt == L"" )
        throw FdoException::Create( NlsMsgGetMain( MAPINFOPROVIDER_SPATIALCONTEXT_EMPTYWKT, "WKT cannot be empty." ));

    FdoStringP right;
    FdoStringP csName;

    // Parse the WKT. Ignore spaces. The same is done PRJ
    if ( m_coordSysWkt.Contains( L"PROJCS" ) )
        right = m_coordSysWkt.Right( L"PROJCS" );
    else if ( m_coordSysWkt.Contains( L"GEOGCS" ) )
        right = m_coordSysWkt.Right( L"GEOGCS" );
    else if ( m_coordSysWkt.Contains( L"LOCAL_CS" ) )
        right = m_coordSysWkt.Right( L"LOCAL_CS" );

    if ( right != L"" )
    {
        FdoStringP right2 = right.Right( L"[" );
        FdoStringP right3 = right2.Right( L"\"" );
        csName = right3.Left( L"\"" );
    }

    if ( csName == L"" )
        throw FdoException::Create( NlsMsgGetMain( MAPINFOPROVIDER_SPATIALCONTEXT_INVALIDWKT, "Invalid WKT. Cannot extract coordinate system name." ) );

    m_scName = m_coordSysName = csName;

    if ( m_scName == L"" )
        throw FdoException::Create( NlsMsgGetMain( MAPINFOPROVIDER_SPATIALCONTEXT_EMPTYNAME, "Spatial context name cannot be empty." ));

    m_connection->CreateSpatialContext(m_scName, m_description,
        m_coordSysName, m_coordSysWkt,
        FdoSpatialContextExtentType_Static, m_extent,
        m_xyTolerance, m_zTolerance);

#pragma message ("ToDo: Support updating spatial contexts")
}
